// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) definits fieldsfirst ansi space safe 
// Source File Name:   SimpleSimulator.java

package jones;

import java.io.PrintStream;
import java.util.*;
import jones.rigid.RigidWorld;

// Referenced classes of package jones:
//            Simulator, SimObject

public class SimpleSimulator
    implements Simulator
{
    private class SimTask extends TimerTask
    {

        private List objects = null;
        private List observables = null;
        private int frameCount = 0;

        public void run()
        {
            updateSimObjects();
            updateObservables();
            nextFrame();
        }

        public synchronized void nextFrame()
        {
            frameCount++;
        }

        public synchronized int getFrameCount()
        {
            return frameCount;
        }

        private synchronized void updateObservables()
        {
            for (int i = 0; i < observables.size(); i++)
            {
                Observable observable = (Observable)observables.get(i);
                observable.notifyObservers();
            }

        }

        public synchronized void addObservable(Observable observable)
        {
            observables.add(((Object) (observable)));
        }

        public synchronized void removeObservable(Observable observable)
        {
            observables.remove(((Object) (observable)));
        }

        private synchronized void updateSimObjects()
        {
            for (int i = 0; i < objects.size(); i++)
            {
                SimObject object = (SimObject)objects.get(i);
                object.update(0.040000000000000001D);
            }

        }

        private synchronized void addSimObject(SimObject object)
        {
            objects.add(((Object) (object)));
        }

        private synchronized void removeSimObject(SimObject object)
        {
            objects.remove(((Object) (object)));
        }



        public SimTask(Collection objects, Collection observables)
        {
            this.objects = ((List) (new ArrayList()));
            this.observables = ((List) (new ArrayList()));
            frameCount = 0;
            this.objects.addAll(objects);
            this.observables.addAll(observables);
        }
    }


    private static final int FRAME_RATE = 25;
    private List objects = null;
    private List observables = null;
    private SimTask simTask = null;
    private RigidWorld world = null;
    private Timer timer = null;
    private boolean isRunning = false;
    private long startTime = 0;

    public SimpleSimulator()
    {
        objects = ((List) (new ArrayList()));
        observables = ((List) (new ArrayList()));
        simTask = new SimTask(((Collection) (objects)), ((Collection) (observables)));
        world = new RigidWorld();
        timer = new Timer();
        isRunning = false;
        startTime = 0L;
    }

    public void addSimObject(SimObject simObject)
    {
        objects.add(((Object) (simObject)));
        if (isRunning)
            simTask.addSimObject(simObject);
    }

    public void removeSimObject(SimObject simObject)
    {
        objects.remove(((Object) (simObject)));
        if (isRunning)
            simTask.removeSimObject(simObject);
    }

    public void addObservable(Observable observable)
    {
        observables.add(((Object) (observable)));
        if (isRunning)
            simTask.addObservable(observable);
    }

    public void removeObservable(Observable observable)
    {
        observables.remove(((Object) (observable)));
        if (isRunning)
            simTask.removeObservable(observable);
    }

    public void start()
    {
        if (!isRunning)
        {
            simTask = new SimTask(((Collection) (objects)), ((Collection) (observables)));
            timer.schedule(((TimerTask) (simTask)), 0L, 40L);
            startTime = System.currentTimeMillis();
            isRunning = true;
        }
    }

    public void stop()
    {
        if (isRunning)
        {
            simTask.cancel();
            isRunning = false;
            long currentTime = System.currentTimeMillis();
            double frameRate = ((double)simTask.getFrameCount() * 1000D) / (double)(currentTime - startTime);
            System.out.println("avg frame rate = " + frameRate);
        }
    }

    public void step()
    {
        if (!isRunning)
        {
            simTask = new SimTask(((Collection) (objects)), ((Collection) (observables)));
            timer.schedule(((TimerTask) (simTask)), 0L);
        }
    }
}
